package com.library.uiframework.slidingclose;

import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;

/**
 * SlidingCloseableHelper.java
 * Created by james on 2015/9/24 14:09.
 * Copyright (c) 2015 james. All rights reserved.
 *
 * @author james.guo
 * @version 1.0.0
 */
public class SlidingCloseableHelper implements OnSlidingCloseListener, OnSlidingScrollListener {
    private SlidingClosableRelativeLayout mSlidingClosableRelativeLayout;
    private SlideHelperSlidingScrollInterface mSlideHelperSlidingScrollInterface;
    private SlideHelperSlidingCloseInterface mSlideHelperSlidingCloseInterface;

    public SlidingClosableRelativeLayout getSlidingClosableRelativeLayout() {
        return mSlidingClosableRelativeLayout;
    }

    public void setSlideHelperSlidingScrollInterface(SlideHelperSlidingScrollInterface slideHelperSlidingScrollInterface) {
        mSlideHelperSlidingScrollInterface = slideHelperSlidingScrollInterface;
    }

    public void setSlideHelperSlidingCloseInterface(SlideHelperSlidingCloseInterface slideHelperSlidingCloseInterface) {
        mSlideHelperSlidingCloseInterface = slideHelperSlidingCloseInterface;
    }

    public void injectFragment(Fragment fragment) {

    }

    public ViewGroup injectView(View contentView) {
        mSlidingClosableRelativeLayout = new SlidingClosableRelativeLayout(contentView.getContext());
        mSlidingClosableRelativeLayout.setSlidingCloseMode(SlidingClosableRelativeLayout.SLIDING_CLOSE_MODE_HORIZONTAL_RIGHT);
        mSlidingClosableRelativeLayout.setOnSlidingCloseListener(this);
        mSlidingClosableRelativeLayout.setOnSlidingScrollListener(this);
        mSlidingClosableRelativeLayout.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return mSlidingClosableRelativeLayout;
    }

    @Override
    public void onSlidingClosed() {
        if (mSlidingClosableRelativeLayout != null) {
            mSlidingClosableRelativeLayout.setVisibility(View.GONE);
            if (mSlideHelperSlidingCloseInterface != null) {
                mSlideHelperSlidingCloseInterface.onSlidingClosed();
            }
        } else {
            if (mSlideHelperSlidingCloseInterface != null) {
                mSlideHelperSlidingCloseInterface.onSlidingClosed();
            }
        }
    }

    @Override
    public void onScrollStarted() {
        if (mSlideHelperSlidingScrollInterface != null) {
            mSlideHelperSlidingScrollInterface.onScrollStarted();
        }
    }

    @Override
    public void onScrollEnded(boolean isCloseState) {
        if (mSlideHelperSlidingScrollInterface != null) {
            mSlideHelperSlidingScrollInterface.onScrollEnded(isCloseState);
        }
    }
}
